package com.inews.business.wangpan.jinshan;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.inews.business.wangpan.jinshan.util.BASE64Encoder;
import com.inews.business.wangpan.jinshan.util.CommUtil;
import com.inews.business.wangpan.jinshan.util.HMACSHA1;
import com.inews.business.wangpan.jinshan.util.UrlSwitch;



/*
 * @author chaoxinghou
 * 快盘服务类
 * QQ:615662165
 */
public class KuaiPan {
	//初始化字典顺序
	//private final String[] dictionarys = {"oauth_consumer_key","oauth_nonce","oauth_signature_method","oauth_timestamp","oauth_token","oauth_version","width","height","type","view","zip","path","root","overwrite","from_path","to_path"};
	//需要把overwrite放置到path，root的前面, by jackguoxz http://blog.sina.com.cn/jackguoxz
	private final String[] dictionarys = {"oauth_consumer_key","oauth_nonce","oauth_signature_method","oauth_timestamp","oauth_token","oauth_version","width","height","type","view","zip","overwrite","path","root","from_path","to_path"};
	private String oauth_consumer_key;
	private String consumer_secret;
	private String oauth_token;
	private String oauth_token_secret;
	private String path;
	private String root;
	private String oauth_nonce;
	private String oauth_timestamp;
	private String requestMethod;
	private String oauth_version;
	private String oauth_signature_method;
	private String overwrite;
	private String from_path;
	private String to_path;
	private int width;
	private int height;
	private String type;
	private String view;
	private int zip;
	
	//本地参数
	//local_file : 下载文件，本地文件存放地址
	public File local_file;
	//记录请求方法
	private String method;


	private List<String[]> list = new ArrayList<String[]>();
	private List<String[]> listpx = new ArrayList<String[]>();

	public KuaiPan(String method) {
		this.requestMethod = UrlSwitch.getMETHOD().get(method);
		this.method = method;
	}
	
	/*
	 * 
	 * 接口说明
	 * 返回请求URL 
	 */
	public String getUrl() {
		if(requestMethod == null) {
			return "服务参数无效。";
		}
		this.list.add(new String[]{"oauth_timestamp",oauth_timestamp});
		this.list.add(new String[]{"oauth_nonce",oauth_nonce});
		for (String dictionary : dictionarys) {
			String[] arr = getValue(dictionary);
			if(arr != null) {
				listpx.add(arr);
			}
		}
		return getAPIURL();
	}
	
	/*
	 * 提交处理请求并返回处理结果
	 */
	public Map<String,Object> submit() throws Exception {
		Map<String,Object> map = new HashMap<String, Object>();
		oauth_timestamp = new Date().getTime()/1000+"";
		oauth_nonce = CommUtil.getRandom();
		String s=getUrl();
		System.out.println(s);
		if(method.equals("download_file")) {
			
			InputStream download=CommUtil.DownFile(s);
			map.put("state", 0 + "");
			map.put("msg", "下载成功");
			map.put("_InputStream", download);
			return map;
		}else if(method.equals("upload_file"))
		{
//			CommUtil.UploadFile(s,local_file + "/" + path.split("/")[path.split("/").length - 1]);
			CommUtil.UploadFile(s,local_file);
			map.put("state", 0 + "");
			map.put("msg", "上传成功");
			return map;
		}
		else {
			map = CommUtil.httpRequest(s, "gbk");
		}
		return map;
	}
	
	private String[] getValue(String key) {
		for (String[] arrstr : list) {
			if(key.equals(arrstr[0])) {
				return arrstr;
			}
		}
		return null;
	}
	
	//拼接生成请求地址
	private String getAPIURL() {
		String url = null;
		try {
			url = requestMethod + "?oauth_signature="+ getSignature();
			for(int i = 0;i < listpx.size(); i++) {
				url += "&" + listpx.get(i)[0] + "=" + listpx.get(i)[1];
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
	
	//生成数字签名
	private String getSignature() throws Exception  {
		StringBuffer buffer = new StringBuffer();
		String path =null;
		//如果是Upload，那http的方式应该是POST， by jackguoxz http://blog.sina.com.cn/jackguoxz
		if(!method.contains("upload")){
			 path = "GET&" + CommUtil.urlToEncode(requestMethod) + "&";
			}
			else
			{
				path = "POST&" + CommUtil.urlToEncode(requestMethod) + "&";			
				
			}
		//String path = "GET&" + CommUtil.urlToEncode(requestMethod) + "&";
		for (String[] stra : listpx) {
			System.out.println(stra[0]+":"+stra[1]);
			if(buffer.length() == 0) {
				buffer.append(CommUtil.urlToEncode(stra[0]) + "=" + CommUtil.urlToEncode(stra[1]));
			}else {
				buffer.append("&" + CommUtil.urlToEncode(stra[0]) + "=" + CommUtil.urlToEncode(stra[1]));
			}
		}
		//生成基串
		String data = path + CommUtil.urlToEncode(buffer.toString());
		System.out.println(data);
		String oauth_signature = CommUtil.urlToEncode(new BASE64Encoder().encode(new HMACSHA1().getHmacSHA1(data, consumer_secret + "&" + oauth_token_secret)));
		return oauth_signature;
	}

	public void setOauth_consumer_key(String oauth_consumer_key) {
		this.oauth_consumer_key = oauth_consumer_key;
		this.list.add(new String[]{"oauth_consumer_key",oauth_consumer_key});
	}

	public void setConsumer_secret(String consumer_secret) {
		this.consumer_secret = consumer_secret;
	}

	public void setOauth_token(String oauth_token) {
		this.oauth_token = oauth_token;
		this.list.add(new String[]{"oauth_token",oauth_token});
	}

	public void setOauth_token_secret(String oauth_token_secret) {
		this.oauth_token_secret = oauth_token_secret;
	}

	public void setPath(String path) {
		this.path = path;
		this.list.add(new String[]{"path",path});
	}

	public void setRoot(String root) {
		this.root = root;
		this.list.add(new String[]{"root",root});
	}

	public void setOauth_version(String oauth_version) {
		this.oauth_version = oauth_version;
		this.list.add(new String[]{"oauth_version",oauth_version});
	}
	public void setOauth_signature_method(String oauth_signature_method) {
		this.oauth_signature_method = oauth_signature_method;
		this.list.add(new String[]{"oauth_signature_method",oauth_signature_method});
	}
	public void setOverwrite(boolean overwrite) {
		if(overwrite) {
			this.overwrite = "True";
			this.list.add(new String[]{"overwrite","true"});
		} else {
			this.overwrite = "False";
			this.list.add(new String[]{"overwrite","false"});
		}
	}
	public void setFrom_path(String from_path) {
		this.from_path = from_path;
		this.list.add(new String[]{"from_path",from_path});
	}

	public void setTo_path(String to_path) {
		this.to_path = to_path;
		this.list.add(new String[]{"to_path",from_path});
	}
	
	public void setWidth(int width) {
		this.width = width;
		this.list.add(new String[]{"width",width + ""});
	}

	public void setHeight(int height) {
		this.height = height;
		this.list.add(new String[]{"height",height + ""});
	}
	public void setType(String type) {
		this.type = type;
		this.list.add(new String[]{"type",type});
	}

	public void setView(String view) {
		this.view = view;
		this.list.add(new String[]{"view",view});
	}

	public void setZip(int zip) {
		this.zip = zip;
		this.list.add(new String[]{"zip",zip + ""});
	}
	
	public void setLocal_file(File local_file) {
		this.local_file = local_file;
	}

	public File getLocal_file() {
		return this.local_file;
	}
}
